<!DOCTYPE html>
<html>
	<head>
		<meta charset='utf-8' />
		<title>deviantART Checker</title>
		<script src="jquery-1.4.3.min.js"></script>
		<script type="text/javascript" >
		
			// listener when settings change
			safari.extension.settings.addEventListener("change", settingsChanged, false);
			
			//listener when extension is enabled the first time (at browser launch)
			safari.application.addEventListener("validate", extensionLaunched, false);
			
			// click on icon
			safari.application.addEventListener("command", commandClick, false);
			
			// timer
			//var updater = safari.extension.globalPage.contentWindow.setInterval( backgroundLoadPage, safari.extension.settings.getItem("updates") * 1000 );
			var updater = safari.extension.globalPage.contentWindow.setInterval( backgroundLoadPage, 10000 );
			
			
			var deviantartURL = "http://my.deviantart.com/messages/";
			var divContainer = null;
			
			function backgroundLoadPage()
			{
				$.ajax({
					url: deviantartURL,
					success: getNewNotifications
				});
			}
			
			function getNewNotifications(data)
			{
				initDiv();
						
				divContainer.innerHTML = data;
				
				var notices = document.getElementsByClassName("oh-l");
				var deviations = document.getElementsByClassName("oh-l");
				var deviantwatch = document.getElementsByClassName("oh-l");
				var feedback = document.getElementsByClassName("oh-l");
			
				var nbNotices = searchForNumber(notices, "Notices");
				var nbDeviations = searchForNumber(deviations, "Deviations");
				var nbDeviantwatch = searchForNumber(deviantwatch, "deviantWATCH Messages");
				var nbFeedback = searchForNumber(feedback, "Feedback");
				
				updateToolbarItem( nbNotices, nbDeviations, nbDeviantwatch, nbFeedback );
				
				divContainer.innerHTML = "";
			}
			
			function initDiv()
			{
				if (divContainer == null)
				{
					divContainer = document.createElement('div');
					document.body.insertBefore(divContainer, null);
				}
			}
			
			function searchForNumber( objectList, keyword )
			{
				for (var i = 0; i < objectList.length; ++i)
				{
					if (objectList[i].title == keyword)
					{
						return parseInt(objectList[i].children[1].innerHTML);
					}
				}
				return 0;
			}
			
			function updateToolbarItem( notices_count, deviations_count, deviantwatch_count, feedback_count )
			{
				$.each(safari.extension.toolbarItems, function(i, v)
				{
					v.badge = notices_count + deviations_count + deviantwatch_count + feedback_count;
					
					var tooltip = "deviantART";
					
					if (notices_count !=0)
					{
						tooltip += "\n" + notices_count + " notice" + (notices_count > 1 ? "s": "");
					}
					if (deviations_count !=0)
					{
						tooltip += "\n" + deviations_count + " deviation" + (deviations_count > 1 ? "s": "");
					}
					if (deviantwatch_count !=0)
					{
						tooltip += "\n" + deviantwatch_count + " deviantWatch" + (deviantwatch_count > 1 ? "es": "");
					}
					if (feedback_count !=0)
					{
						tooltip += "\n" + feedback_count + " feedback" + (feedback_count > 1 ? "s": "");
					}
					
					v.toolTip = tooltip;
				});
			}
			
			function settingsChanged(event)
			{
				if (event.key == "updates")
				{
					safari.extension.globalPage.contentWindow.clearInterval(updater);
					updater = safari.extension.globalPage.contentWindow.setInterval( "backgroundLoadPage()", event.newValue * 1000);
				}
			}
			
			function extensionLaunched(event)
			{
				safari.application.removeEventListener("validate", extensionLaunched, false);
				backgroundLoadPage();
			}
			
			function commandClick(event)
			{
				if (event.command == "daNewTab" )
				{
				// check if DeviantART is open somewhere
					for (i in safari.application.browserWindows)
					{
						for (j in safari.application.browserWindows[i].tabs)
						{
							if (safari.application.browserWindows[i].tabs[j].url && safari.application.browserWindows[i].tabs[j].url.indexOf(deviantartURL) == 0)
							{
								var browserWindow = safari.application.browserWindows[i];
	
								browserWindow.activate();
								browserWindow.tabs[j].activate();
								return;
							}
						}
					}
	
					var newTab = safari.application.activeBrowserWindow.openTab();
					newTab.url = deviantartURL;
				}
			}
			
		</script>
	</head>
</html>